package com.baidu.health.service.impl;

import com.baidu.health.mapper.TbIntegralVariationMapper;
import com.baidu.health.mapper.TbUserMapper;
import com.baidu.health.pojo.ResultResponse;
import com.baidu.health.pojo.TbIntegralVariation;
import com.baidu.health.pojo.TbUser;
import com.baidu.health.service.TbIntegralVariationService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 */
@Service
public class TbIntegralVariationServiceImpl extends ServiceImpl<TbIntegralVariationMapper, TbIntegralVariation>
    implements TbIntegralVariationService{

    @Autowired
    private TbIntegralVariationMapper tbIntegralVariationMapper;
    @Autowired
    private TbUserMapper tbUserMapper;

    @Override
    public ResultResponse IntegralSelect(Integer userId) {
        QueryWrapper queryWrapper=new QueryWrapper();
        if(userId==0){
        }else{
            queryWrapper.eq("user_id",userId);
        }
        queryWrapper.orderByDesc("create_time");
        List<TbIntegralVariation> list = tbIntegralVariationMapper.selectList(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            TbUser tbUser = tbUserMapper.selectById(list.get(i).getUserId());
            list.get(i).setUserName(tbUser.getUserName());
        }
        return ResultResponse.SUCCESS(list);
    }

    @Override
    public ResultResponse userVariation(TbIntegralVariation tbIntegralVariation) {
        Integer userId = tbIntegralVariation.getUserId();
        Integer userVariation = tbIntegralVariation.getUserVariation();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        queryWrapper.orderByDesc("create_time");
        List<TbIntegralVariation> list = tbIntegralVariationMapper.selectList(queryWrapper);
        if(list!=null &&list.size()>0) {
            TbIntegralVariation tbIntegralVariation1 = list.get(0);
            TbIntegralVariation variation=new TbIntegralVariation();
            variation.setUserId(userId);
            variation.setUserGrade(tbIntegralVariation.getUserGrade()+tbIntegralVariation1.getUserGrade());
            variation.setUserVariation(tbIntegralVariation.getUserVariation());
            variation.setCreateTime(new Date());
            if (variation.getUserGrade() < 0) {
                return ResultResponse.FAILED(500, "您的积分不足");
            }
            tbIntegralVariationMapper.insert(variation);
            TbUser tbUser=new TbUser();
            tbUser.setUserId(userId);
            tbUser.setUserGrade(variation.getUserGrade());
            tbUserMapper.updateById(tbUser);
            return ResultResponse.SUCCESS();
        }else{
            TbIntegralVariation variation=new TbIntegralVariation();
            variation.setUserId(userId);
            variation.setUserGrade(userVariation);
            variation.setUserVariation(userVariation);
            variation.setCreateTime(new Date());
            if(userVariation<0){
                return ResultResponse.FAILED(500,"积分不够");
            }
            tbIntegralVariationMapper.insert(variation);
            TbUser tbUser=new TbUser();
            tbUser.setUserId(userId);
            tbUser.setUserGrade(variation.getUserGrade());
            tbUserMapper.updateById(tbUser);
            return ResultResponse.SUCCESS();
        }
    }

    @Override
    public ResultResponse IntegralDelete(Integer userId) {
        tbIntegralVariationMapper.deleteById(userId);
        return ResultResponse.SUCCESS();
    }

    @Override
    public ResultResponse IntegralDeletes(Integer userId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        List<TbIntegralVariation> list = tbIntegralVariationMapper.selectList(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            tbIntegralVariationMapper.deleteById(list.get(i).getIntegralId());
        }
        return ResultResponse.SUCCESS();
    }
}




